# -*- coding: utf-8 -*-
# @Time : 2021/7/13 20:36
# @Author : feimaotuiya
# @File : utils.py
# @Project : flask67


import hashlib
import os
from setting import DATA_PATH, BASE_PATH, CASE_PATH


def md5_code(password):
    '''密码进行md5加密'''
    md5 = hashlib.md5()
    md5.update(password.encode('utf-8'))
    return md5.hexdigest()


def response_data(data):
    '''对返回的数据进行处理'''
    return data.replace('":', '=').replace('="', '=')


def gen_case(file_yaml_name, txt_name):
    '''根据yaml文件，模板文件生成用例'''
    # 将yaml文件的文件名提出来
    file_name = file_yaml_name.split('.')[0]
    class_name = file_name.title().replace('_', '')
    data_name = file_name
    case_name = file_name
    # 将模板文件内容写入测试用例文件
    template = os.path.join(BASE_PATH, txt_name)
    with open(template, encoding='utf-8') as f:
        content = f.read()
    content = content % {
        "class_name": class_name,
        "data_name": data_name,
        "case_name": case_name
    }
    test_case_name = os.path.join(CASE_PATH, 'test_{}.py'.format(file_name))
    with open(test_case_name, 'w', encoding='utf-8') as f:
        f.write(content)


def create_case_file():
    file_lists = os.listdir(DATA_PATH)
    template_file = os.path.join(BASE_PATH, 'case_template_dr.txt')
    for fList in file_lists:
        if fList.endswith('.yaml') or fList.endswith('.yml'):
            data_file = fList.replace('.yaml', '').replace('.yml', '')
            test_method_name = data_file.lower()
            test_class_name = test_method_name.title()
            with open(template_file, 'r', encoding='utf-8') as f:
                content = f.read() % {
                    'class_name': test_class_name,
                    'method_name': test_method_name,
                    'data_file': data_file
                }
            test_case_file = 'test_%s.py' % data_file
            with open(os.path.join(CASE_PATH, test_case_file), 'w', encoding='utf-8') as f:
                f.write(content)


def set_res_data(res):
    return res.replace('":', '=').replace('="', '=')


def create_cases_file():
    file_lists = os.listdir(DATA_PATH)
    template_file = os.path.join(BASE_PATH, 'case_template_wlw.txt')
    for flist in file_lists:
        if flist.endswith('.yaml') or flist.endswith('.yml'):
            data_file = flist.replace('.yaml', '').replace('.yml', '')
            test_method_name = data_file.lower()
            test_class_name = test_method_name.capitalize()
            with open(template_file, 'r', encoding='utf-8') as tamp:
                content = tamp.read() % {'class_name': test_class_name, 'case_name': test_method_name,
                                         'data_name': data_file}
            test_case_file = 'test_%s.py' % data_file
            with open(os.path.join(CASE_PATH, test_case_file), 'w', encoding='utf-8')as f:
                f.write(content)


if __name__ == '__main__':
    print(md5_code('123456'))
